//
//  AccountTool.m
//  aiyu_2
//
//  Created by ibokan on 14-10-14.
//  Copyright (c) 2014年 ibokan. All rights reserved.
//



/*
 //检查是否有缓存的用户数据
 Account *currentAcount=[AccountTool sharedAccountTool].currentAccount;
 
 NSLog(@"%@     %@     relation:%@",currentAcount.uid,currentAcount.accessToken,currentAcount.relation);
 
 
 if (currentAcount) {
 
 }
 
 //存储用户数据的方法：
 
 Account *acc=[[Account alloc]init];
 acc.accessToken=accessToken;
 acc.uid=uid;
 acc.name=JSON[0][kName];
 acc.sex=JSON[0][kSex];
 acc.userImage=JSON[0][kUserImage];
 acc.relation=JSON[0][kRelation];
 acc.telephone=JSON[0][kTel];
 
 
 [[AccountTool sharedAccountTool] addAccounts:acc];
 

 
 
 */

#import "AccountTool.h"
#import "Account.h"
#import "config.h"
@implementation AccountTool
singleton_implementation(AccountTool)
-(id)init
{
    if(self=[super init])
    {
        self.accounts=[NSKeyedUnarchiver unarchiveObjectWithFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"accounts.dat"]];
        
        self.currentAccount=[NSKeyedUnarchiver unarchiveObjectWithFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"currentAccount.dat"]];
        if (self.accounts==nil) {
            self.accounts=[NSMutableDictionary dictionary];
        }
    }
    return self;
}


-(void)addAccounts:(Account *)account
{
    [self.accounts setObject:account forKey:account.uid];
    self.currentAccount=account;
    
    [NSKeyedArchiver archiveRootObject:self.accounts toFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"accounts.dat"]];
    
    [NSKeyedArchiver archiveRootObject:self.currentAccount toFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"currentAccount.dat"]];
    
}


-(Account *)accountWithUid:(NSString *)uid{
    Account *acc =   [self.accounts objectForKey:uid];
    return acc;
}



-(void)signOut:(Account *)account{
    
    self.currentAccount=nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager isDeletableFileAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"currentAccount.dat"]]) {
        [fileManager removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"currentAccount.dat"] error:nil];
    }
}

-(void) tuichu:(Account *)account
{
    //先退出在添加一个空得数据
    [self signOut:account];
    
    Account *acc;
    
    acc=[[Account alloc]init];
    acc.cardType=@"";
    acc.custIdent=@"";
    acc.cardNo=@"";
    acc.custName=@"";
    acc.custIType=@"";
    acc.custPhone=@"";
    acc.custAddr=@"";
    acc.custSex=@"";
    acc.custFlag=@"";
    acc.modiDate=@"";
    acc.pwd=@"";
    acc.YKTCardBalance=@"";
    acc.YKTCardJF=@"";
    acc.HaveYKTCard=@"";
    acc.NickName=@"";
    acc.HeadImg=@"";
    acc.WeXinId=@"";
    acc.Email=@"";
    acc.CheckTime=@"";
    
    acc.WorkGuid=@"{015A26A7-19B5-4F5E-A910-23F6A93A04B3}";
    acc.xhtpwd=@"6NP8DLD26F26H82R6V4J4B2X";
    acc.uid=@"96696021";
   
    //将数据清理后保存到沙盒中
    [self addAccounts:acc];
    
    



}





@end
